package com.ugoodtech.mdcc.core.service.impl;

import com.mysema.query.BooleanBuilder;
import com.ugoodtech.mdcc.core.Constants;
import com.ugoodtech.mdcc.core.domain.*;
import com.ugoodtech.mdcc.core.domain.VstarServiceReport.*;
import com.ugoodtech.mdcc.core.helper.SearchVoId;
import com.ugoodtech.mdcc.core.model.entity.Departments;
import com.ugoodtech.mdcc.core.model.entity.DepartmentsExample;
import com.ugoodtech.mdcc.core.model.entity.StationRegularServiceReportExample;
import com.ugoodtech.mdcc.core.model.entity.StationReportExample;
import com.ugoodtech.mdcc.core.model.entity.Stations;
import com.ugoodtech.mdcc.core.model.entity.StationsExample;
import com.ugoodtech.mdcc.core.model.entity.StationsExample.Criteria;
import com.ugoodtech.mdcc.core.model.entity.UserProfiles;
import com.ugoodtech.mdcc.core.model.entity.UsersExample;
import com.ugoodtech.mdcc.core.model.mapper.DepartmentsMapper;
import com.ugoodtech.mdcc.core.model.mapper.StationColorReportMapper;
import com.ugoodtech.mdcc.core.model.mapper.StationRegularServiceReportMapper;
import com.ugoodtech.mdcc.core.model.mapper.StationReportMapper;
import com.ugoodtech.mdcc.core.model.mapper.StationsMapper;
import com.ugoodtech.mdcc.core.model.mapper.UsersMapper;
import com.ugoodtech.mdcc.core.repository.*;
import com.ugoodtech.mdcc.core.service.DepartmentCoreService;
import com.ugoodtech.mdcc.core.service.SearchPermissionsService;
import com.ugoodtech.mdcc.core.service.MybatisStationReportService;
import com.ugoodtech.mdcc.core.service.MybatisUserService;
import com.ugoodtech.mdcc.core.service.VstarCoreServiceReportService;

import java.util.function.Function;
import java.util.stream.Collectors;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class VstarCoreServiceReportServiceImpl implements VstarCoreServiceReportService {

    private static final Logger logger = LoggerFactory.getLogger(VstarCoreServiceReportServiceImpl.class);

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private StationColorReportRepository stationColorReportRepository;

    @Autowired
    private UserProfileRepository userProfileRepository;

    @Autowired
    private StationReportRepository stationReportRepository;

    @Autowired
    private StationRegularServiceReportRepository stationRegularServiceReportRepository;

    @Autowired
    private QuestionAnswerOptionRepository questionAnswerOptionRepository;

    @Autowired
    private DepartmentCoreService departmentCoreService;

    @Autowired
    private MybatisUserService mybatisUserService;
    @Autowired
    private MybatisStationReportService mybatisStationReportService;

    @Autowired
    private StationsMapper stationsMapper;
    @Autowired
    private UsersMapper usersMapper;

    private Integer countPerson;

    private List<Station> stationList = new ArrayList<>();

    private List<String[]> otherCustomerStatList = new ArrayList<>();

    private List<String[]> otherCustomerTypeList = new ArrayList<>();

    private List<String[]> otherPaintBrandList = new ArrayList<>();

    private List<String[]> otherMaintainBigProblem = new ArrayList<>();

    private List<String[]> otherStationAppeal = new ArrayList<>();

    @Autowired
    private SearchPermissionsService searchPermissionsService;
    @Autowired
    private CustomerRegularReportRepository customerRegularReportRepository;
    @Autowired
    private StationRegularServiceReportMapper stationRegularServiceReportMapper;
    @Autowired
    private StationColorReportMapper stationColorReportMapper;

    @Autowired
    private DepartmentsMapper departmentsMapper;

    @Autowired
    private StationReportMapper stationReportMapper;

    ///////////////////////////////改////////////////////////////////////////

    //统计常规服务报告
    @Override
    public VstarServiceReport statReport(String province, String city, String brand, String category, String stationName,
                                         Long serviceStartTime, Long serviceEndTime, Long startTime, Long endTime, String serviceType,
                                         Integer serviceNum, String reportStatus,String userName, Boolean type, String serviceReportStatus, Long userId, Boolean paintType,
                                         String customerStat, String customerType, String paintBrand, String productPosition, String stationNum, String department,String payService,String sentTime,String serviceTime) {
        List<StationReport> reports = getStationReports(province,city, brand, category, stationName, serviceStartTime, serviceEndTime, startTime, endTime, serviceType,
                serviceNum, reportStatus, userName, type, serviceReportStatus, userId, paintType, customerStat, customerType, paintBrand, productPosition, stationNum, department,payService,sentTime,serviceTime);
        List<StationReport> serviceReportList = new ArrayList<>();
        List<Long> serviceReportIdList = new ArrayList<>();

        for (StationReport stationReport : reports) {
            serviceReportIdList.add(stationReport.getId());
        }
            List<StationRegularServiceReport> answerOfReports =
                stationRegularServiceReportRepository.selectAnswerOfReports(serviceReportIdList, 354L);
        Map<String, StationRegularServiceReport> idToReportMap = answerOfReports.stream().collect(
            Collectors.toMap(StationRegularServiceReport::getReportId, Function.identity(), (s1,
            s2)->s1));

        for (StationReport stationReport : reports) {
            StationRegularServiceReport answer = idToReportMap.get(
                String.valueOf(stationReport.getId()));
            if (null == answer) {
                serviceReportList.add(stationReport);
            }
        }
        // for (StationReport stationReport : reports) {
        //     StationRegularServiceReport answer = stationRegularServiceReportRepository.selectAnswer(stationReport.getId(), 354L);
        //     if (null == answer) {
        //         serviceReportList.add(stationReport);
        //     }
        // }
        otherCustomerStatList = new ArrayList<>();
        otherCustomerTypeList = new ArrayList<>();
        otherPaintBrandList = new ArrayList<>();
        otherMaintainBigProblem = new ArrayList<>();
        otherStationAppeal = new ArrayList<>();
        countPerson = serviceReportList.size();
        stationList = new ArrayList<>();
        logger.info("查出的常规服务报告有：" + countPerson + "份");
        for (StationReport report : serviceReportList) {
            if (stationList.contains(report.getStation())) {
                continue;
            } else {
                stationList.add(report.getStation());
            }
        }
        logger.info("有效的维修站数量为：" + stationList.size() + "个");
        VstarServiceReport vstarServiceReport = new VstarServiceReport();
        vstarServiceReport.setCountPerson(countPerson);
        //销售区域
        vstarServiceReport.setProvinceLsit(getProvinceList(serviceReportList));
        //客户状态
        vstarServiceReport.setCustomerStatList(getCustomerStatList(serviceReportList));
        //其他客户状态
        vstarServiceReport.setOthercustomerStatList(otherCustomerStatList);
        //客户类型 111
        vstarServiceReport.setCustomerTypeList(getCustomerTypeList(serviceReportList));
        //其他客户类型
        vstarServiceReport.setOtherCustomerTypeList(otherCustomerTypeList);
        //统计维修站
        vstarServiceReport.setVstarServiceReport_serviceType(getServiceType(serviceReportList));
        //统计品牌
        if (StringUtils.isNotEmpty(brand) && StringUtils.isEmpty(category)) {
            Integer IBS = 0;
            Integer BPC = 0;
            Integer FBS = 0;
            Integer DEALERS = 0;
            String[] brandArray = brand.split(",");
            Map<String, Integer> countBrandMap = new HashMap<>();
            List<String[]> brandStr = new ArrayList<>();
            List<String[]> otherBrandArray = new ArrayList<>();
            Map<String, Integer> otherCountBrandMap = new HashMap<>();
            for (StationReport report : serviceReportList) {
                for (String brandName : brandArray) {
                    if (StringUtils.isNotEmpty(brandName)) {
                        if (report.getStation().getCategory().equals(Constants.FOR_S) && report.getStation().getBrand().indexOf(brandName) != -1) {
                            if (countBrandMap.containsKey(brandName)) {
                                Integer value = countBrandMap.get(brandName);
                                countBrandMap.put(brandName, value + 1);
                            } else {
                                countBrandMap.put(brandName, 1);
                            }
                        }
                    }
                }
                if (report.getStation().getCategory().equals(Constants.IBS)) {
                    String[] brandName = report.getStation().getBrand().split(",");
                    for (String stationBrand : brandName) {
                        QuestionAnswerOption answerOption = questionAnswerOptionRepository.findByName(stationBrand);
                        if (answerOption == null) {
                            if (otherCountBrandMap.containsKey(stationBrand)) {
                                Integer value = otherCountBrandMap.get(stationBrand);
                                otherCountBrandMap.put(stationBrand, value + 1);
                            } else {
                                otherCountBrandMap.put(stationBrand, 1);
                            }
                        }
                    }
                    IBS += 1;
                }
                if (report.getStation().getCategory().equals(Constants.BPC)) {
                    String[] brandName = report.getStation().getBrand().split(",");
                    for (String stationBrand : brandName) {
                        QuestionAnswerOption answerOption = questionAnswerOptionRepository.findByName(stationBrand);
                        if (answerOption == null) {
                            if (otherCountBrandMap.containsKey(stationBrand)) {
                                Integer value = otherCountBrandMap.get(stationBrand);
                                otherCountBrandMap.put(stationBrand, value + 1);
                            } else {
                                otherCountBrandMap.put(stationBrand, 1);
                            }
                        }
                    }
                    BPC += 1;
                }
                if (report.getStation().getCategory().equals(Constants.FBS)) {
                    String[] brandName = report.getStation().getBrand().split(",");
                    for (String stationBrand : brandName) {
                        QuestionAnswerOption answerOption = questionAnswerOptionRepository.findByName(stationBrand);
                        if (answerOption == null) {
                            if (otherCountBrandMap.containsKey(stationBrand)) {
                                Integer value = otherCountBrandMap.get(stationBrand);
                                otherCountBrandMap.put(stationBrand, value + 1);
                            } else {
                                otherCountBrandMap.put(stationBrand, 1);
                            }
                        }
                    }
                    FBS += 1;
                }
                if (report.getStation().getCategory().equals(Constants.DEALERS)) {
                    String[] brandName = report.getStation().getBrand().split(",");
                    for (String stationBrand : brandName) {
                        QuestionAnswerOption answerOption = questionAnswerOptionRepository.findByName(stationBrand);
                        if (answerOption == null) {
                            if (otherCountBrandMap.containsKey(stationBrand)) {
                                Integer value = otherCountBrandMap.get(stationBrand);
                                otherCountBrandMap.put(stationBrand, value + 1);
                            } else {
                                otherCountBrandMap.put(stationBrand, 1);
                            }
                        }
                    }
                    DEALERS += 1;
                }
            }
            String[] str = new String[2];
            for (Map.Entry<String, Integer> entry : countBrandMap.entrySet()) {
                str = new String[3];
                str[0] = entry.getKey();
                str[1] = entry.getValue().toString();
                str[2] = "";
                brandStr.add(str);
            }
            if (IBS != 0) {
                str = new String[3];
                str[0] = Constants.IBS;
                str[1] = String.valueOf(IBS);
                str[2] = "";
                brandStr.add(str);
            }
            if (BPC != 0) {
                str = new String[3];
                str[0] = Constants.BPC;
                str[1] = String.valueOf(BPC);
                str[2] = "";
                brandStr.add(str);
            }
            if (FBS != 0) {
                str = new String[3];
                str[0] = Constants.FBS;
                str[1] = String.valueOf(FBS);
                str[2] = "";
                brandStr.add(str);
            }
            if (DEALERS != 0) {
                str = new String[3];
                str[0] = Constants.DEALERS;
                str[1] = String.valueOf(DEALERS);
                str[2] = "";
                brandStr.add(str);
            }
            for (Map.Entry<String, Integer> entry : otherCountBrandMap.entrySet()) {
                String[] otherBrand = new String[3];
                otherBrand[0] = entry.getKey();
                otherBrand[1] = entry.getValue().toString();
                otherBrand[2] = "";
                otherBrandArray.add(otherBrand);
            }
            VstarServiceReport_Brand vstarServiceReport_brand = new VstarServiceReport_Brand();
            vstarServiceReport_brand.setBrandArray(brandStr);
            vstarServiceReport_brand.setOtherBrandList(otherBrandArray);
            vstarServiceReport.setVstarServiceReport_brand(vstarServiceReport_brand);
        } else {
            if (StringUtils.isNotEmpty(brand)) {
                String[] brandArray = brand.split(",");
                vstarServiceReport.setVstarServiceReport_brand(getBrand(brandArray, serviceReportList));
            } else {
                vstarServiceReport.setVstarServiceReport_brand(getBrand(null, serviceReportList));
            }

        }

        //油漆品牌 111
        vstarServiceReport.setPaintBrandList(getPaintBrandList(serviceReportList));
        //其他油漆品牌
        vstarServiceReport.setOtherPaintBrandList(otherPaintBrandList);
        //品牌定位
        vstarServiceReport.setProductPositionList(getProductPositionList(serviceReportList));
        //该维修站目前钣喷维修流程中面对最大问题
        vstarServiceReport.setMaintainBigProblem(getMaintainBigProblemList(serviceReportList));
        //其他该维修站目前钣喷维修流程中面对最大问题
        vstarServiceReport.setOtherMaintainBigProblem(otherMaintainBigProblem);
        //该维修站的主要诉求 111
        vstarServiceReport.setStationAppeal(getStationAppeal(serviceReportList));
        //其他该维修站的主要诉求
        vstarServiceReport.setOtherStationAppeal(otherStationAppeal);
        //统计服务目标
        vstarServiceReport.setVstarServiceReport_target(getTarget(serviceReportList));
        //统计服务结果
        vstarServiceReport.setVstarServiceReport_results(getResults(serviceReportList));
        return vstarServiceReport;
    }
    //统计常规服务报告
    @Override
    public VstarServiceReport statReport(String provinces, String brand, String category, String name,
                                         Long serviceStartTime, Long serviceEndTime, Long startTime, Long endTime, Long serviceType,
                                         Integer serviceNum, String userName, String isFollowUp, String followType, Long userId, String userType, Boolean isCustomer,
                                         Long customerStat, Long customerType, String paintBrand, String productPosition, String stationNum, Long departmentId) {
        List<StationReport> reports = getStationReport(provinces, brand, category, name, serviceStartTime, serviceEndTime, startTime, endTime, serviceType,
                serviceNum, userName, isFollowUp, followType, userId, userType, isCustomer, customerStat, customerType, paintBrand, productPosition, stationNum, departmentId);
        List<StationReport> reportList = new ArrayList<>();
        for (StationReport stationReport : reports) {
            StationRegularServiceReport answer = stationRegularServiceReportRepository.selectAnswer(stationReport.getId(), 354L);
            if (null == answer) {
                reportList.add(stationReport);
            }
        }
        otherCustomerStatList = new ArrayList<>();
        otherCustomerTypeList = new ArrayList<>();
        otherPaintBrandList = new ArrayList<>();
        otherMaintainBigProblem = new ArrayList<>();
        otherStationAppeal = new ArrayList<>();
        countPerson = reportList.size();
        stationList = new ArrayList<>();
        logger.info("查出的常规服务报告有：" + countPerson + "份");
        for (StationReport report : reportList) {
            if (stationList.contains(report.getStation())) {
                continue;
            } else {
                stationList.add(report.getStation());
            }
        }
        logger.info("有效的维修站数量为：" + stationList.size() + "个");
        VstarServiceReport vstarServiceReport = new VstarServiceReport();
        vstarServiceReport.setCountPerson(countPerson);
        //销售区域
        vstarServiceReport.setProvinceLsit(getProvinceList(reportList));
        //客户状态
        vstarServiceReport.setCustomerStatList(getCustomerStatList(reportList));
        //其他客户状态
        vstarServiceReport.setOthercustomerStatList(otherCustomerStatList);
        //客户类型
        vstarServiceReport.setCustomerTypeList(getCustomerTypeList(reportList));
        //其他客户类型
        vstarServiceReport.setOtherCustomerTypeList(otherCustomerTypeList);
        //统计维修站
        vstarServiceReport.setVstarServiceReport_serviceType(getServiceType(reportList));
        //统计品牌
        if (StringUtils.isNotEmpty(brand) && StringUtils.isEmpty(category)) {
            Integer IBS = 0;
            Integer BPC = 0;
            Integer FBS = 0;
            Integer DEALERS = 0;
            String[] brandArray = brand.split(",");
            Map<String, Integer> countBrandMap = new HashMap<>();
            List<String[]> brandStr = new ArrayList<>();
            List<String[]> otherBrandArray = new ArrayList<>();
            Map<String, Integer> otherCountBrandMap = new HashMap<>();
            for (StationReport report : reportList) {
                for (String brandName : brandArray) {
                    if (StringUtils.isNotEmpty(brandName)) {
                        if (report.getStation().getCategory().equals(Constants.FOR_S) && report.getStation().getBrand().indexOf(brandName) != -1) {
                            if (countBrandMap.containsKey(brandName)) {
                                Integer value = countBrandMap.get(brandName);
                                countBrandMap.put(brandName, value + 1);
                            } else {
                                countBrandMap.put(brandName, 1);
                            }
                        }
                    }
                }
                if (report.getStation().getCategory().equals(Constants.IBS)) {
                    String[] brandName = report.getStation().getBrand().split(",");
                    for (String stationBrand : brandName) {
                        QuestionAnswerOption answerOption = questionAnswerOptionRepository.findByName(stationBrand);
                        if (answerOption == null) {
                            if (otherCountBrandMap.containsKey(stationBrand)) {
                                Integer value = otherCountBrandMap.get(stationBrand);
                                otherCountBrandMap.put(stationBrand, value + 1);
                            } else {
                                otherCountBrandMap.put(stationBrand, 1);
                            }
                        }
                    }
                    IBS += 1;
                }
                if (report.getStation().getCategory().equals(Constants.BPC)) {
                    String[] brandName = report.getStation().getBrand().split(",");
                    for (String stationBrand : brandName) {
                        QuestionAnswerOption answerOption = questionAnswerOptionRepository.findByName(stationBrand);
                        if (answerOption == null) {
                            if (otherCountBrandMap.containsKey(stationBrand)) {
                                Integer value = otherCountBrandMap.get(stationBrand);
                                otherCountBrandMap.put(stationBrand, value + 1);
                            } else {
                                otherCountBrandMap.put(stationBrand, 1);
                            }
                        }
                    }
                    BPC += 1;
                }
                if (report.getStation().getCategory().equals(Constants.FBS)) {
                    String[] brandName = report.getStation().getBrand().split(",");
                    for (String stationBrand : brandName) {
                        QuestionAnswerOption answerOption = questionAnswerOptionRepository.findByName(stationBrand);
                        if (answerOption == null) {
                            if (otherCountBrandMap.containsKey(stationBrand)) {
                                Integer value = otherCountBrandMap.get(stationBrand);
                                otherCountBrandMap.put(stationBrand, value + 1);
                            } else {
                                otherCountBrandMap.put(stationBrand, 1);
                            }
                        }
                    }
                    FBS += 1;
                }
                if (report.getStation().getCategory().equals(Constants.DEALERS)) {
                    String[] brandName = report.getStation().getBrand().split(",");
                    for (String stationBrand : brandName) {
                        QuestionAnswerOption answerOption = questionAnswerOptionRepository.findByName(stationBrand);
                        if (answerOption == null) {
                            if (otherCountBrandMap.containsKey(stationBrand)) {
                                Integer value = otherCountBrandMap.get(stationBrand);
                                otherCountBrandMap.put(stationBrand, value + 1);
                            } else {
                                otherCountBrandMap.put(stationBrand, 1);
                            }
                        }
                    }
                    DEALERS += 1;
                }
            }
            String[] str = new String[2];
            for (Map.Entry<String, Integer> entry : countBrandMap.entrySet()) {
                str = new String[3];
                str[0] = entry.getKey();
                str[1] = entry.getValue().toString();
                str[2] = "";
                brandStr.add(str);
            }
            if (IBS != 0) {
                str = new String[3];
                str[0] = Constants.IBS;
                str[1] = String.valueOf(IBS);
                str[2] = "";
                brandStr.add(str);
            }
            if (BPC != 0) {
                str = new String[3];
                str[0] = Constants.BPC;
                str[1] = String.valueOf(BPC);
                str[2] = "";
                brandStr.add(str);
            }
            if (FBS != 0) {
                str = new String[3];
                str[0] = Constants.FBS;
                str[1] = String.valueOf(FBS);
                str[2] = "";
                brandStr.add(str);
            }
            if (DEALERS != 0) {
                str = new String[3];
                str[0] = Constants.DEALERS;
                str[1] = String.valueOf(DEALERS);
                str[2] = "";
                brandStr.add(str);
            }
            for (Map.Entry<String, Integer> entry : otherCountBrandMap.entrySet()) {
                String[] otherBrand = new String[3];
                otherBrand[0] = entry.getKey();
                otherBrand[1] = entry.getValue().toString();
                otherBrand[2] = "";
                otherBrandArray.add(otherBrand);
            }
            VstarServiceReport_Brand vstarServiceReport_brand = new VstarServiceReport_Brand();
            vstarServiceReport_brand.setBrandArray(brandStr);
            vstarServiceReport_brand.setOtherBrandList(otherBrandArray);
            vstarServiceReport.setVstarServiceReport_brand(vstarServiceReport_brand);
        } else {
            if (StringUtils.isNotEmpty(brand)) {
                String[] brandArray = brand.split(",");
                vstarServiceReport.setVstarServiceReport_brand(getBrand(brandArray, reportList));
            } else {
                vstarServiceReport.setVstarServiceReport_brand(getBrand(null, reportList));
            }

        }

        //油漆品牌
        vstarServiceReport.setPaintBrandList(getPaintBrandList(reportList));
        //其他油漆品牌
        vstarServiceReport.setOtherPaintBrandList(otherPaintBrandList);
        //品牌定位
        vstarServiceReport.setProductPositionList(getProductPositionList(reportList));
        //该维修站目前钣喷维修流程中面对最大问题
        vstarServiceReport.setMaintainBigProblem(getMaintainBigProblemList(reportList));
        //其他该维修站目前钣喷维修流程中面对最大问题
        vstarServiceReport.setOtherMaintainBigProblem(otherMaintainBigProblem);
        //该维修站的主要诉求
        vstarServiceReport.setStationAppeal(getStationAppeal(reportList));
        //其他该维修站的主要诉求
        vstarServiceReport.setOtherStationAppeal(otherStationAppeal);
        //统计服务目标
        vstarServiceReport.setVstarServiceReport_target(getTarget(reportList));
        //统计服务结果
        vstarServiceReport.setVstarServiceReport_results(getResults(reportList));
        return vstarServiceReport;
    }

    //将Map转List
    private List<String[]> toListStringArray(Map<String, Integer> map) {
        List<String[]> strList = new ArrayList<>();
        String[] str = new String[3];
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            str = new String[3];
            str[0] = entry.getKey();
            str[1] = entry.getValue().toString();
            str[2] = "";
            strList.add(str);
        }
        return strList;
    }

    private Map<Long, Map<Long, StationColorReport>> getStationColorReport_AnswerOther(List<StationReport> reportList,
        List<Long> questionId) {

        List<Long> stationIdList = reportList.stream().map(StationReport::getStationId).distinct()
            .filter(Objects::nonNull).collect(Collectors.toList());


        QStationReport qStationReport = QStationReport.stationReport;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qStationReport.deleted.eq(false));
        builder.and(qStationReport.enabled.eq(true));
        builder.and(qStationReport.station.id.in(stationIdList));
        builder.and(qStationReport.category.eq(Constants.TECHCOLOR_REPORT));
        List<StationReport> techReportList = (List<StationReport>) stationReportRepository.findAll(
            builder);

        List<Long> reportIdList = techReportList.stream().map(StationReport::getId).distinct()
            .filter(Objects::nonNull).collect(Collectors.toList());

        List<StationColorReport> colorReportList =
            stationColorReportRepository.selectReport(reportIdList, questionId);

        return convertListToMap(colorReportList);
    }

    public static <T extends StationColorReport> Map<Long, Map<Long, T>> convertListToMap(List<T> list) {
        return list.stream().collect(Collectors.groupingBy(
            T::getReportIdNotReport,
            Collectors.toMap(
                T::getQuestionId,
                item -> item
            )
        ));
    }

    // private String getStationColorReport_AnswerOther(Stations stations, Long questionId) {
    //     if (stations != null && stations.getId() != null && stations.getId() != 0) {
    //         Long stationId = stations.getId();
    //         QStationReport qStationReport = QStationReport.stationReport;
    //         BooleanBuilder builder = new BooleanBuilder();
    //         builder.and(qStationReport.deleted.eq(false));
    //         builder.and(qStationReport.enabled.eq(true));
    //         builder.and(qStationReport.station.id.eq(stationId));
    //         builder.and(qStationReport.category.eq(Constants.TECHCOLOR_REPORT));
    //         StationReport report = stationReportRepository.findOne(builder);
    //         if (report == null) {
    //             return null;
    //         }
    //         StationColorReport colorReport = stationColorReportRepository.selectReport(report.getId(), questionId);
    //         if (colorReport != null && StringUtils.isNotEmpty(colorReport.getAnswerOther())) {
    //             return colorReport.getAnswerOther();
    //         }
    //     }
    //     return null;
    // }

    //销售区域
    private List<String[]> getProvinceList(List<StationReport> reportList) {
        Map<String, Integer> provinceMap = new HashMap<>();

        List<Long> stationReportIdList = reportList.stream().map(StationReport::getId)
            .collect(Collectors.toList());
        List<Stations> stationsList = mybatisStationReportService.listStations(stationReportIdList);
        if (CollectionUtils.isEmpty(stationsList)) {
            return new ArrayList<>(0);
        }
        for (Stations stations: stationsList) {
            if (StringUtils.isNotEmpty(stations.getProvince())) {
                String province = stations.getProvince();
                if (provinceMap.containsKey(province)) {
                    Integer value = provinceMap.get(province);
                    provinceMap.put(province, value + 1);
                } else {
                    provinceMap.put(province, 1);
                }
            }
        }
        return toListStringArray(provinceMap);
    }

    //客户状态
    private List<String[]> getCustomerStatList(List<StationReport> reportList) {
        Map<String, Integer> customerStatMap = new HashMap<>();
        Map<String, Integer> otherMap = new HashMap<>();
        QuestionAnswerOption answerOption = null;
        Map<Long, Map<Long, StationColorReport>> reportIdToQuestionIdToQuestionMap = getStationColorReport_AnswerOther(
            reportList, Arrays.asList(184L));
        List<Long> customerStatList = new ArrayList<>(64);
        for (StationReport report : reportList) {
            if (report.getStation().getCustomerStat() != null
                && report.getStation().getCustomerStat() != 0) {
                Long customerStatId = report.getStation().getCustomerStat();
                customerStatList.add(customerStatId);
            }
        }
        if (CollectionUtils.isEmpty(customerStatList)) {
            return new ArrayList<>(0);
        }
        BooleanBuilder idBuilder = new BooleanBuilder();
        idBuilder.and(QQuestionAnswerOption.questionAnswerOption.id.in(customerStatList));
        List<QuestionAnswerOption> answerList = (List<QuestionAnswerOption>) questionAnswerOptionRepository.findAll(
            idBuilder);
        Map<Long, QuestionAnswerOption> idToAnswerMap = answerList.stream()
            .collect(Collectors.toMap(QuestionAnswerOption::getId,
                Function.identity()));
        for (StationReport report : reportList) {
            if (report.getStation().getCustomerStat() != null && report.getStation().getCustomerStat() != 0) {
                Long customerStatId = report.getStation().getCustomerStat();
                answerOption = idToAnswerMap.get(customerStatId);
                if (answerOption != null) {
                    if (customerStatMap.containsKey(answerOption.getName())) {
                        Integer value = customerStatMap.get(answerOption.getName());
                        customerStatMap.put(answerOption.getName(), value + 1);
                    } else {
                        customerStatMap.put(answerOption.getName(), 1);
                    }
                }
                if (customerStatId == 362L) {
                    // todo 其他客户状态
                    String answer = reportIdToQuestionIdToQuestionMap.getOrDefault(
                        report.getId(), new HashMap<>(0)).getOrDefault(184L,
                        new StationColorReport()).getAnswerOther();
                    if (answer == null) {
                        continue;
                    }
                    if (otherMap.containsKey(answer)) {
                        Integer value = otherMap.get(answer);
                        otherMap.put(answer, value + 1);
                    } else {
                        otherMap.put(answer, 1);
                    }
                }
            }
        }
        if (!otherMap.isEmpty()) {
            otherCustomerStatList = toListStringArray(otherMap);
        }
        return toListStringArray(customerStatMap);
    }

    //客户类型
    private List<String[]> getCustomerTypeList(List<StationReport> reportList) {
        Map<String, Integer> customerTypeMap = new HashMap<>();
        Map<String, Integer> otherMap = new HashMap<>();
        QuestionAnswerOption answerOption = null;
        Map<Long, Map<Long, StationColorReport>> reportIdToQuestionIdToQuestionMap = getStationColorReport_AnswerOther(
            reportList, Arrays.asList(185L));

        Set<Long> customerTypeIdSet = new HashSet<>();
        for (StationReport report : reportList) {
            if (report.getStation().getCustomerType() != null
                && report.getStation().getCustomerType() != 0) {
                Long customerTypeId = report.getStation().getCustomerType();
                customerTypeIdSet.add(customerTypeId);
            }
        }
        List<QuestionAnswerOption> byAnswerIdList = questionAnswerOptionRepository.findByAnswerIdList(
            new ArrayList<>(customerTypeIdSet));
        Map<Long, QuestionAnswerOption> idToAnswerMap = byAnswerIdList.stream()
            .collect(Collectors.toMap(QuestionAnswerOption::getId, Function.identity()));
        for (StationReport report : reportList) {
            if (report.getStation().getCustomerType() != null && report.getStation().getCustomerType() != 0) {
                Long customerTypeId = report.getStation().getCustomerType();
                answerOption = idToAnswerMap.get(customerTypeId);
                if (customerTypeMap.containsKey(answerOption.getName())) {
                    Integer value = customerTypeMap.get(answerOption.getName());
                    customerTypeMap.put(answerOption.getName(), value + 1);
                } else {
                    customerTypeMap.put(answerOption.getName(), 1);
                }
                if (customerTypeId == 366L) {
                    // todo 其他客户类型
                    String answer = reportIdToQuestionIdToQuestionMap.getOrDefault(
                        report.getId(), new HashMap<>(0)).getOrDefault(185L,
                        new StationColorReport()).getAnswerOther();
                    if (answer == null) {
                        continue;
                    }
                    if (otherMap.containsKey(answer)) {
                        Integer value = otherMap.get(answer);
                        otherMap.put(answer, value + 1);
                    } else {
                        otherMap.put(answer, 1);
                    }
                }
            }
        }
        if (!otherMap.isEmpty()) {
            otherCustomerTypeList = toListStringArray(otherMap);
        }
        return toListStringArray(customerTypeMap);
    }

    //油漆品牌
    private List<String[]> getPaintBrandList(List<StationReport> reportList) {
        Map<String, Integer> paintBrandMap = new HashMap<>();
        Map<String, Integer> otherMap = new HashMap<>();
        QuestionAnswerOption answerOption = null;

        Set<Long> paintBrandSet = new HashSet<>();
        for (StationReport report : reportList) {
            if (StringUtils.isNotEmpty(report.getStation().getPaintBrand())) {
                String[] paintBrandids = report.getStation().getPaintBrand().split(",");
                for (String id : paintBrandids) {
                    paintBrandSet.add(Long.valueOf(id));
                }
            }
        }

        List<QuestionAnswerOption> byAnswerIdList = questionAnswerOptionRepository.findByAnswerIdList(
            new ArrayList<>(paintBrandSet));
        Map<Long, QuestionAnswerOption> idToAnswerMap = byAnswerIdList.stream()
            .collect(Collectors.toMap(QuestionAnswerOption::getId, Function.identity()));
        Map<Long, Map<Long, StationColorReport>> reportIdToQuestionIdToQuestionMap = getStationColorReport_AnswerOther(
            reportList, Arrays.asList(13L));
        for (StationReport report : reportList) {
            if (StringUtils.isNotEmpty(report.getStation().getPaintBrand())) {
                String[] paintBrandids = report.getStation().getPaintBrand().split(",");
                for (String id : paintBrandids) {
                    answerOption = idToAnswerMap.get(Long.valueOf(id));
                    if (answerOption!=null && answerOption.getName()!=null) {
                        if (paintBrandMap.containsKey(answerOption.getName())) {
                            Integer value = paintBrandMap.get(answerOption.getName());
                            paintBrandMap.put(answerOption.getName(), value + 1);
                        } else {
                            paintBrandMap.put(answerOption.getName(), 1);
                        }
                    }
                    if (id.equals("280")) {
                        //todo 其他的油漆品牌
                        String answer =reportIdToQuestionIdToQuestionMap.getOrDefault(
                            report.getId(), new HashMap<>(0)).getOrDefault(13L,
                            new StationColorReport()).getAnswerOther();
                        if (answer == null) {
                            continue;
                        }
                        if (otherMap.containsKey(answer)) {
                            Integer value = otherMap.get(answer);
                            otherMap.put(answer, value + 1);
                        } else {
                            otherMap.put(answer, 1);
                        }
                    }
                }
            }
        }
        if (!otherMap.isEmpty()) {
            otherPaintBrandList = toListStringArray(otherMap);
        }
        return toListStringArray(paintBrandMap);
    }

    //品牌定位
    private List<String[]> getProductPositionList(List<StationReport> reportList) {
        Map<String, Integer> productPositionMap = new HashMap<>();
        QuestionAnswerOption answerOption = null;
        List<String> productPositionidList = new ArrayList<>();
        for (StationReport report : reportList) {
            if (StringUtils.isNotEmpty(report.getStation().getProductPosition())) {
                String[] productPositionids = report.getStation().getProductPosition().split(",");
                productPositionidList.addAll(Arrays.asList(productPositionids));
            }
        }
            List<Long> productPositionidLongList =
                productPositionidList.stream().map(Long::valueOf).distinct()
                .collect(Collectors.toList());
            BooleanBuilder booleanBuilder = new BooleanBuilder();
            QQuestionAnswerOption questionAnswerOption = QQuestionAnswerOption.questionAnswerOption;
            booleanBuilder.and(questionAnswerOption.id.in(productPositionidLongList));
            List<QuestionAnswerOption> answerOptionRepositoryAll = (List<QuestionAnswerOption>)
                questionAnswerOptionRepository.findAll(
                booleanBuilder);
            Map<Long, QuestionAnswerOption> idToOptionMap = answerOptionRepositoryAll.stream()
                .collect(Collectors.toMap(QuestionAnswerOption::getId, Function.identity()));

            for (StationReport report : reportList) {
            if (StringUtils.isNotEmpty(report.getStation().getProductPosition())) {
                String[] productPositionids = report.getStation().getProductPosition().split(",");
                for (String id : productPositionids) {
                    answerOption = idToOptionMap.get(Long.valueOf(id));
                    if (answerOption!=null && answerOption.getName()!=null) {
                        if (productPositionMap.containsKey(answerOption.getName())) {
                            Integer value = productPositionMap.get(answerOption.getName());
                            productPositionMap.put(answerOption.getName(), value + 1);
                        } else {
                            productPositionMap.put(answerOption.getName(), 1);
                        }
                    }
                }
            }
        }
        return toListStringArray(productPositionMap);
    }

    //目前颜色准确度
    private List<String[]> getColorAccuracyList(List<StationReport> reportList) {
        Map<String, Integer> colorAccuracyMap = new HashMap<>();
        QuestionAnswerOption answerOption = null;
        for (StationReport report : reportList) {
            // todo 目前颜色准确度
        }
        return null;
    }

    //维修站目前钣喷维修流程中面对最大问题
    private List<String[]> getMaintainBigProblemList(List<StationReport> reportList) {
        Map<String, Integer> maintainBigProblemMap = new HashMap<>();
        Map<String, Integer> otherMap = new HashMap<>();
        QuestionAnswerOption answerOption = null;
        StationColorReport stationColorReport = null;

        QStationReport qStationReport = QStationReport.stationReport;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qStationReport.deleted.eq(false));
        builder.and(qStationReport.enabled.eq(true));
        builder.and(qStationReport.category.eq(Constants.TECHCOLOR_REPORT));
        builder.and(qStationReport.auditing.eq(Constants.AUDIT_TRUE));
        List<Long> stationIdList = reportList.stream().map(item -> item.getStation().getId()).distinct()
            .collect(Collectors.toList());
        builder.and(qStationReport.station.id.in(stationIdList));
        List<StationReport> stationReportFiltered = (List<StationReport>)stationReportRepository.findAll(builder);
        Map<Long, StationReport> stationIdToStationReportMap = stationReportFiltered.stream()
            .collect(Collectors.toMap(StationReport::getStationId, Function.identity()));

        // 56号映射问题
        List<Long> reportIdList = stationReportFiltered.stream().map(StationReport::getId)
            .collect(Collectors.toList());
        List<StationColorReport> stationColorReport56List =
            stationColorReportRepository.selectReport(reportIdList, 56L);
        Map<Long, StationColorReport> id56ToAnswerMap = stationColorReport56List.stream()
            .collect(Collectors.toMap(StationColorReport::getReportIdNotReport, Function.identity()));

        List<String> problemAllId = new ArrayList<>(16);
        stationColorReport56List
            .forEach(item -> problemAllId.addAll(Arrays.asList(item.getAnswerId().split(","))));
        List<Long> problemIdLongList =
            problemAllId.stream().map(Long::valueOf).distinct().collect(Collectors.toList());

        QQuestionAnswerOption questionAnswerOption = QQuestionAnswerOption.questionAnswerOption;
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(questionAnswerOption.id.in(problemIdLongList));
        List<QuestionAnswerOption> optionList =
            (List<QuestionAnswerOption>)questionAnswerOptionRepository.findAll(booleanBuilder);
        Map<Long, QuestionAnswerOption> idToOptionMap = optionList.stream()
            .collect(Collectors.toMap(QuestionAnswerOption::getId,
                Function.identity()));

        Map<Long, Map<Long, StationColorReport>> reportIdToQuestionIdToQuestionMap = getStationColorReport_AnswerOther(
            new ArrayList<>(stationIdToStationReportMap.values()),
            Arrays.asList(56L));

        for (StationReport report : reportList) {
            StationReport stationReport = stationIdToStationReportMap.get(
                report.getStation().getId());
            if (stationReport == null) {
                logger.info("ID为：" + report.getStation().getId() + "的维修站没有基础报告");
                continue;
            }
            stationColorReport = id56ToAnswerMap.get(stationReport.getId());
            if (stationColorReport != null && StringUtils.isNotEmpty(stationColorReport.getAnswerId())) {
                String[] problemIds = stationColorReport.getAnswerId().split(",");
                for (String id : problemIds) {
                    if (StringUtils.isNotEmpty(id)) {
                        answerOption = idToOptionMap.get(Long.valueOf(id));
                        if (maintainBigProblemMap.containsKey(answerOption.getName())) {
                            Integer value = maintainBigProblemMap.get(answerOption.getName());
                            maintainBigProblemMap.put(answerOption.getName(), value + 1);
                        } else {
                            maintainBigProblemMap.put(answerOption.getName(), 1);
                        }
                        if (id.equals("266")) {
                            // todo 其他面对最大问题
                            String answer = reportIdToQuestionIdToQuestionMap.getOrDefault(
                                stationReport.getId(), new HashMap<>(0)).getOrDefault(56L,
                                new StationColorReport()).getAnswerOther();
                            if (answer == null) {
                                continue;
                            }
                            if (otherMap.containsKey(answer)) {
                                Integer value = otherMap.get(answer);
                                otherMap.put(answer, value + 1);
                            } else {
                                otherMap.put(answer, 1);
                            }
                        }
                    }
                }
            }
        }
        if (!otherMap.isEmpty()) {
            otherMaintainBigProblem = toListStringArray(otherMap);
        }
        return toListStringArray(maintainBigProblemMap);
    }

    //维修站的主要诉求
    private List<String[]> getStationAppeal(List<StationReport> reportList) {
        Map<String, Integer> stationAppealMap = new HashMap<>();
        Map<String, Integer> otherMap = new HashMap<>();
        QuestionAnswerOption answerOption = null;
        StationColorReport stationColorReport = null;

        List<Long> stationIdList =
            reportList.stream().map(StationReport::getStationId).filter(Objects::nonNull).distinct()
            .collect(Collectors.toList());

        List<Long> reportIdList =
            reportList.stream().map(StationReport::getId).filter(Objects::nonNull).distinct()
                .collect(Collectors.toList());

        if (CollectionUtils.isEmpty(stationIdList)) {
            return new ArrayList<>(0);
        }



        QStationReport qStationReport = QStationReport.stationReport;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qStationReport.deleted.eq(false));
        builder.and(qStationReport.enabled.eq(true));
        builder.and(qStationReport.category.eq(Constants.TECHCOLOR_REPORT));
        builder.and(qStationReport.auditing.eq(Constants.AUDIT_TRUE));
        builder.and(qStationReport.station.id.in(stationIdList));
        List<StationReport> stationTechReportList =
            (List<StationReport>)stationReportRepository.findAll(builder);
        List<Long> techReportIdList = stationTechReportList.stream().map(StationReport::getId)
            .collect(Collectors.toList());
        Map<Long, StationReport> idToTechMap = stationTechReportList.stream()
            .collect(Collectors.toMap(StationReport::getStationId, Function.identity()));
        List<StationColorReport> stationColorReportList =
            stationColorReportRepository.selectReport(techReportIdList, 59L);
        Map<Long, StationColorReport> idTo59ColorReportMap = stationColorReportList.stream()
            .collect(Collectors.toMap(StationColorReport::getReportIdNotReport,
                Function.identity()));

        Set<Long> appealIdList = new HashSet<>(4);
        for (StationReport report : reportList) {
            StationReport stationReport = idToTechMap.get(report.getStationId());
            if (stationReport == null) {
                logger.info("ID为：" + report.getStation().getId() + "的维修站没有基础报告");
                continue;
            }
            stationColorReport = idTo59ColorReportMap.get(stationReport.getId());
            if (stationColorReport != null && StringUtils.isNotEmpty(
                stationColorReport.getAnswerId())) {
                String[] appealIds = stationColorReport.getAnswerId().split(",");
                for (String id : appealIds) {
                    if (StringUtils.isNotEmpty(id)) {
                        appealIdList.add(Long.valueOf(id));
                    }
                }
            }
        }
        List<QuestionAnswerOption> byAnswerIdList = questionAnswerOptionRepository.findByAnswerIdList(
            new ArrayList<>(appealIdList));
        Map<Long, QuestionAnswerOption> idToAnswerMap = byAnswerIdList.stream()
            .collect(Collectors.toMap(QuestionAnswerOption::getId,
                Function.identity()));
        Map<Long, Map<Long, StationColorReport>> reportIdToQuestionIdToQuestionMap = getStationColorReport_AnswerOther(
            new ArrayList<>(idToTechMap.values()),
            Arrays.asList(59L));

        for (StationReport report : reportList) {
            StationReport stationReport = idToTechMap.get(report.getStationId());
            if (stationReport == null) {
                logger.info("ID为：" + report.getStation().getId() + "的维修站没有基础报告");
                continue;
            }
            stationColorReport = idTo59ColorReportMap.get(stationReport.getId());
            if (stationColorReport != null && StringUtils.isNotEmpty(stationColorReport.getAnswerId())) {
                String[] appealIds = stationColorReport.getAnswerId().split(",");
                for (String id : appealIds) {
                    if (StringUtils.isNotEmpty(id)) {
                        answerOption = idToAnswerMap.get(Long.valueOf(id));
                        if (stationAppealMap.containsKey(answerOption.getName())) {
                            Integer value = stationAppealMap.get(answerOption.getName());
                            stationAppealMap.put(answerOption.getName(), value + 1);
                        } else {
                            stationAppealMap.put(answerOption.getName(), 1);
                        }
                        if (id.equals("272")) {
                            // todo 其他主要诉求
                            String answer =
                                reportIdToQuestionIdToQuestionMap.getOrDefault(
                                    stationReport.getId(), new HashMap<>(0)).getOrDefault(59L,
                                    new StationColorReport()).getAnswerOther();
                            if (answer == null) {
                                continue;
                            }
                            if (otherMap.containsKey(answer)) {
                                Integer value = otherMap.get(answer);
                                otherMap.put(answer, value + 1);
                            } else {
                                otherMap.put(answer, 1);
                            }
                        }
                    }
                }
            }
        }
        if (!otherMap.isEmpty()) {
            otherStationAppeal = toListStringArray(otherMap);
        }
        return toListStringArray(stationAppealMap);
    }


    //统计维修站类型
    private VstarServiceReport_ServiceType getServiceType(List<StationReport> reportList) {
        VstarServiceReport_ServiceType serviceType = new VstarServiceReport_ServiceType();
        Long BPC = 0L;
        Long FBS = 0L;
        Long fourS = 0L;
        Long IBS = 0L;
        Long DEALERS = 0L;
        for (StationReport report : reportList) {
            if (report.getStation().getCategory().equals(Constants.BPC)) {
                BPC += 1;
            }
            if (report.getStation().getCategory().equals(Constants.FBS)) {
                FBS += 1;
            }
            if (report.getStation().getCategory().equals(Constants.FOR_S)) {
                fourS += 1;
            }
            if (report.getStation().getCategory().equals(Constants.IBS)) {
                IBS += 1;
            }
            if (report.getStation().getCategory().equals(Constants.DEALERS)) {
                DEALERS += 1;
            }
        }
        serviceType.setBPC(BPC.intValue());
        serviceType.setFBS(FBS.intValue());
        serviceType.setFourS(fourS.intValue());
        serviceType.setIBS(IBS.intValue());
        serviceType.setDEALERS(DEALERS.intValue());
        serviceType.setCountPerson(BPC.intValue() + FBS.intValue() + fourS.intValue() + IBS.intValue() + DEALERS.intValue());
        return serviceType;
    }


    //统计品牌
    private VstarServiceReport_Brand getBrand(String[] brandArray, List<StationReport> reportList) {
        List<QuestionAnswerOption> allBrand = questionAnswerOptionRepository.findBrand();
        Set<String> allBrandSet = allBrand.stream().map(QuestionAnswerOption::getName)
            .collect(Collectors.toSet());
        List<String> brandNames = new ArrayList<>();
        for (StationReport report : reportList) {
            if (report.getStation().getCategory().equals(Constants.FOR_S)) {
                String[] brandName = report.getStation().getBrand().split(",");
                for (String brand : brandName) {
                    if (brandArray != null) {
                        for (String str : brandArray) {
                            if (!brand.equals(str)) {
                                continue;
                            } else {
                                if (StringUtils.isNotEmpty(brand)) {
                                    brandNames.add(brand);
                                }
                            }
                        }
                    } else {
                        if (StringUtils.isNotEmpty(brand)) {
                            brandNames.add(brand);
                        }
                    }
                }
            } else {
                if (report.getStation().getCategory().equals(Constants.IBS)) {
                    String[] brandName = report.getStation().getBrand().split(",");
                    for (String brand : brandName) {
                        if (brandArray != null) {
                            for (String str : brandArray) {
                                if (brand.equals(str)) {
                                    continue;
                                } else {
                                    if (StringUtils.isNotEmpty(brand)) {
                                        if (!allBrandSet.contains(brand)){
                                            brandNames.add(brand);
                                        }
                                        break;
                                    }
                                }
                            }
                        } else {
                            if (StringUtils.isNotEmpty(brand)) {
                                if (!allBrandSet.contains(brand)){
                                    brandNames.add(brand);
                                }
                            }
                        }
                    }
                    brandNames.add(Constants.IBS);
                }
                if (report.getStation().getCategory().equals(Constants.BPC)) {
                    String[] brandName = report.getStation().getBrand().split(",");
                    for (String brand : brandName) {
                        if (brandArray != null) {
                            for (String str : brandArray) {
                                if (brand.equals(str)) {
                                    continue;
                                } else {
                                    if (StringUtils.isNotEmpty(brand)) {
                                        if (!allBrandSet.contains(brand)){
                                            brandNames.add(brand);
                                        }
                                        break;
                                    }
                                }
                            }
                        } else {
                            if (StringUtils.isNotEmpty(brand)) {
                                if (!allBrandSet.contains(brand)){
                                    brandNames.add(brand);
                                }
                            }
                        }
                    }
                    brandNames.add(Constants.BPC);
                }
                if (report.getStation().getCategory().equals(Constants.FBS)) {
                    String[] brandName = report.getStation().getBrand().split(",");
                    for (String brand : brandName) {
                        if (brandArray != null) {
                            for (String str : brandArray) {
                                if (brand.equals(str)) {
                                    continue;
                                } else {
                                    if (StringUtils.isNotEmpty(brand)) {
                                        if (!allBrandSet.contains(brand)){
                                            brandNames.add(brand);
                                        }
                                        break;
                                    }
                                }
                            }
                        } else {
                            if (StringUtils.isNotEmpty(brand)) {
                                if (!allBrandSet.contains(brand)){
                                    brandNames.add(brand);
                                }
                            }
                        }
                    }
                    brandNames.add(Constants.FBS);
                }
                if (report.getStation().getCategory().equals(Constants.DEALERS)) {
                    String[] brandName = report.getStation().getBrand().split(",");
                    for (String brand : brandName) {
                        if (brandArray != null) {
                            for (String str : brandArray) {
                                if (brand.equals(str)) {
                                    continue;
                                } else {
                                    if (StringUtils.isNotEmpty(brand)) {
                                        if (!allBrandSet.contains(brand)){
                                            brandNames.add(brand);
                                        }
                                        break;
                                    }
                                }
                            }
                        } else {
                            if (StringUtils.isNotEmpty(brand)) {
                                if (!allBrandSet.contains(brand)){
                                    brandNames.add(brand);
                                }
                            }
                        }
                    }
                    brandNames.add(Constants.DEALERS);
                }
            }
        }
        return countBrand(brandNames);
    }

    private VstarServiceReport_Brand countBrand(List<String> brandNames) {
        Map<String, Integer> brandMap = new HashMap<>();
        for (String brand : brandNames) {
            if (brandMap.containsKey(brand)) {
                Integer value = brandMap.get(brand);
                brandMap.put(brand, value + 1);
            } else {
                brandMap.put(brand, 1);
            }
        }
        VstarServiceReport_Brand brand = new VstarServiceReport_Brand();
        List<String[]> brandList = new ArrayList<>();
        List<String[]> otherBrandList = new ArrayList<>();
        Map<String, Integer> otherbrandMap = new HashMap<>();
        for (Map.Entry<String, Integer> entry : brandMap.entrySet()) {
            if (entry.getKey().equals(Constants.IBS) || entry.getKey().equals(Constants.FBS) || entry.getKey().equals(Constants.BPC)) {
                continue;
            }
            String[] brandArray = new String[3];
            QuestionAnswerOption answerOption = questionAnswerOptionRepository.findByName(entry.getKey());
            if (answerOption != null) {
                brandArray[0] = entry.getKey();
                brandArray[1] = entry.getValue().toString();
                brandArray[2] = "";
                brandList.add(brandArray);
            } else {
                if (otherbrandMap.containsKey("其他品牌")) {
                    Integer value = otherbrandMap.get("其他品牌");
                    otherbrandMap.put("其他品牌", value + entry.getValue());
                } else {
                    otherbrandMap.put("其他品牌", entry.getValue());
                }
                String[] otherBrandArray = new String[3];
                otherBrandArray[0] = entry.getKey();
                otherBrandArray[1] = entry.getValue() + "";
                otherBrandArray[2] = "";
                otherBrandList.add(otherBrandArray);
            }
        }
        for (Map.Entry<String, Integer> entry : brandMap.entrySet()) {
            if (entry.getKey().equals(Constants.IBS) || entry.getKey().equals(Constants.FBS) || entry.getKey().equals(Constants.BPC)) {
                String[] otherBrand = new String[3];
                otherBrand[0] = entry.getKey();
                otherBrand[1] = entry.getValue().toString();
                otherBrand[2] = "";
                brandList.add(otherBrand);
            }
        }
        if (otherbrandMap != null && !otherbrandMap.isEmpty()) {
            String[] otherBrand = new String[3];
            otherBrand[0] = "其他品牌";
            otherBrand[1] = otherbrandMap.get("其他品牌").toString();
            otherBrand[2] = "";
            brandList.add(otherBrand);
        }
        brand.setOtherBrandList(otherBrandList);
        brand.setBrandArray(brandList);
        brand.setCountPerson(countPerson);
        return brand;
    }


    //统计服务目标
    private VstarServiceReport_Target getTarget(List<StationReport> reports) {
        List<String> targetIdList = new ArrayList<>();
        List<StationReport> reportList = new ArrayList<>();
        for (StationReport report : reports) {
            String[] targetIds = report.getServiceTypAnswerIds().split(",");
            for (String target : targetIds) {
                if (StringUtils.isNotEmpty(target)) {
                    targetIdList.add(target);
                    if (target.equals("191")) {
                        reportList.add(report);
                    }
                }
            }
        }
        VstarServiceReport_Target targetList = countTarget(targetIdList);
        targetList.setOtherTarget(targetOther(reportList));
        return targetList;
    }

    private VstarServiceReport_Target countTarget(List<String> targetIdList) {
        Map<Long, Integer> targetMap = new HashMap<>();
        for (String target : targetIdList) {
            if (targetMap.containsKey(Long.valueOf(target))) {
                Integer value = targetMap.get(Long.valueOf(target));
                targetMap.put(Long.valueOf(target), value + 1);
            } else {
                targetMap.put(Long.valueOf(target), 1);
            }
        }
        VstarServiceReport_Target target = new VstarServiceReport_Target();
        List<String[]> targetList = new ArrayList<>();

        QQuestionAnswerOption questionAnswerOption = QQuestionAnswerOption.questionAnswerOption;
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(questionAnswerOption.id.in(targetMap.keySet()));
        List<QuestionAnswerOption> questionAnswerOptions = (List<QuestionAnswerOption>)questionAnswerOptionRepository.findAll(
            booleanBuilder);
        Map<Long, QuestionAnswerOption> idToOptionMap = questionAnswerOptions.stream()
            .collect(Collectors.toMap(QuestionAnswerOption::getId, Function.identity()));
        for (Map.Entry<Long, Integer> entry : targetMap.entrySet()) {
            String[] targetArray = new String[3];
            QuestionAnswerOption answerOption = idToOptionMap.get(entry.getKey());
            if (answerOption != null) {
                targetArray[0] = answerOption.getName();
                targetArray[1] = entry.getValue().toString();
                targetArray[2] = "";
                targetList.add(targetArray);
            }
        }
        target.setTarget(targetList);
        target.setCountPerson(countPerson);
        return target;
    }

    private List<String[]> targetOther(List<StationReport> reportList) {
        List<String[]> otherTargerList = new ArrayList<>();
        if (CollectionUtils.isEmpty(reportList)) {
            return new ArrayList<>(0);
        }
        List<Long> reportIdList = reportList.stream().map(StationReport::getId).distinct()
            .collect(Collectors.toList());
        List<StationRegularServiceReport> stationRegularServiceReportList = stationRegularServiceReportRepository.selectQuestionOther(
            reportIdList, 155L);
        Map<Long, StationRegularServiceReport> id155Map = stationRegularServiceReportList.stream()
            .collect(Collectors.toMap(StationRegularServiceReport::getId, Function.identity()));

        List<Long> userIdList = reportList.stream().map(StationReport::getUserId)
            .filter(Objects::nonNull).distinct().collect(
                Collectors.toList());

        List<UserProfile> byUserId = userProfileRepository.findByUserId(userIdList);
        Map<Long, UserProfile> userIdToProfileMap = byUserId.stream()
            .collect(Collectors.toMap(UserProfile::getUserId, Function.identity()));

        for (StationReport report : reportList) {
//            if (report.getServiceTypAnswerIds().indexOf("191") == -1) {
//                continue;
//            }
            StationRegularServiceReport serviceReport = id155Map.get(report.getId());

            if (serviceReport == null) {
                continue;
            }
            String realName = "";
            if (report.getUserId() != null) {
                UserProfile userProfile = userIdToProfileMap.get(report.getUserId());
                if (userProfile != null) {
                    if (StringUtils.isNotEmpty(userProfile.getRealName())) {
                        realName = userProfile.getRealName();
                    }
                }
            }
            String[] otherTarger = new String[6];
            otherTarger[0] = report.getStation().getName();
            otherTarger[1] = report.getStation().getStationNum();
            otherTarger[2] = report.getCreationTimeStr();
            otherTarger[3] = serviceReport.getAnswerOther();
            otherTarger[4] = realName;
            otherTarger[5] = "";
            otherTargerList.add(otherTarger);
        }
        return otherTargerList;
    }

    //统计服务结果
    private VstarServiceReport_Results getResults(List<StationReport> reports) {
        VstarServiceReport_Results results = new VstarServiceReport_Results();
        Long noComplete_UnableToFollowUp = 0L;
        Long noComplete_NeedToFollowUp = 0L;
        Long complete = 0L;
        Long complete_NeedOthersToFollowUp = 0L;
        Long manageProductNum = 0L;
        Long plan = 0L;
        List<Long> reportIdList = reports.stream().map(StationReport::getId).distinct()
            .collect(Collectors.toList());

        Map<Long, List<com.ugoodtech.mdcc.core.model.entity.StationRegularServiceReport>> idToStationRegularServiceReport =
            mybatisStationReportService.mapIdToStationRegularServiceReport(reportIdList);
        for (StationReport report : reports) {
            List<com.ugoodtech.mdcc.core.model.entity.StationRegularServiceReport> answerList = idToStationRegularServiceReport.getOrDefault(
                report.getId(), new ArrayList<>(0));
            answerList = answerList.stream().filter(item -> Objects.equals(item.getQuestionId(),
                156L)).collect(Collectors.toList());
            answerList = answerList.stream().filter(item -> Objects.nonNull(item.getAnswerId())).collect(
                Collectors.toList());
            Map<String, List<com.ugoodtech.mdcc.core.model.entity.StationRegularServiceReport>> answerIdMap = answerList.stream()
                .collect(Collectors.groupingBy(
                    com.ugoodtech.mdcc.core.model.entity.StationRegularServiceReport::getAnswerId));
            boolean isTrue = false;
            Long result = Long.valueOf(
                answerIdMap.getOrDefault("179", Collections.EMPTY_LIST).size());
            if (result > 0) {
                isTrue = true;
                noComplete_UnableToFollowUp += result;
            }
            result = Long.valueOf(
                answerIdMap.getOrDefault("180", Collections.EMPTY_LIST).size());
            if (result > 0) {
                isTrue = true;
                noComplete_NeedToFollowUp += result;
            }
            result = Long.valueOf(
                answerIdMap.getOrDefault("181", Collections.EMPTY_LIST).size());
            if (result > 0) {
                isTrue = true;
                complete += result;
            }
            result = Long.valueOf(
                answerIdMap.getOrDefault("182", Collections.EMPTY_LIST).size());
            if (result > 0) {
                isTrue = true;
                complete_NeedOthersToFollowUp += result;
            }
            result = Long.valueOf(
                answerIdMap.getOrDefault("246", Collections.EMPTY_LIST).size());
            if (result > 0) {
                isTrue = true;
                manageProductNum += result;
            }
            result = Long.valueOf(
                answerIdMap.getOrDefault("354", Collections.EMPTY_LIST).size());
            if (result > 0) {
                isTrue = true;
                plan += result;
            }
            if (!isTrue) {
                System.out.println(report.getId());
            }
        }
        List<VstarServiceReport_CompleteFollowUp> completeFollowUps = new ArrayList<>();
        List<VstarServiceReport_UnFinshedNoFollowUp> unFinshedNoFollowUps = new ArrayList<>();
        List<VstarServiceReport_UnFinshedFollowUp> unFinshedFollowUps = new ArrayList<>();

        List<String[]> noComplete_UnFollowUp = new ArrayList<>();
        List<String[]> noComplete_needFollowUp = new ArrayList<>();
        List<String[]> completeReport = new ArrayList<>();
        List<String[]> complete_needFollowUp = new ArrayList<>();
        List<String[]> manageProduct = new ArrayList<>();
        List<String[]> planReport = new ArrayList<>();

        // 筛选问题答案


        QStationRegularServiceReport regularServiceReport = QStationRegularServiceReport.stationRegularServiceReport;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(regularServiceReport.deleted.eq(false));
        builder.and(regularServiceReport.stationReport.id.in(reportIdList));
        builder.and(regularServiceReport.questionId.eq(156L));
        List<StationRegularServiceReport> stationRegularServiceReportList =
            (List<StationRegularServiceReport>) stationRegularServiceReportRepository.findAll(builder);

        Map<String, List<StationRegularServiceReport>> idToRegularServiceReportMap =
            stationRegularServiceReportList.stream()
                .collect(Collectors.groupingBy(item -> item.getStationReport().getId().toString()));

        List<Long> userIdList = reports.stream().map(StationReport::getUserId).distinct()
            .collect(Collectors.toList());

        List<UserProfiles> mybatisUserProfiles = mybatisUserService.listUserProfile(userIdList);

        List<UserProfile> userProfiles = new ArrayList<>();
        mybatisUserProfiles.forEach(item -> {
            UserProfile profile = new UserProfile();
            profile.setUserId(item.getUserId());
            profile.setRealName(item.getRealname());
            userProfiles.add(profile);
        });

        Map<Long, UserProfile> idToUserProfileMap = userProfiles.stream()
            .collect(Collectors.toMap(UserProfile::getUserId, Function.identity()));

        List<Departments> departmentsList = departmentsMapper.selectByExample(
            new DepartmentsExample());

        List<Department> departments = new ArrayList<>();
        departmentsList.forEach(item -> {
            Department department = new Department();
            department.setName(item.getName());
            department.setId(item.getId());
            departments.add(department);
        });

        Map<Long, Department> idToDepartmentMap = departments.stream()
            .collect(Collectors.toMap(Department::getId, Function.identity()));

        List<Long> stationIdList = reports.stream().map(StationReport::getStationId)
            .collect(Collectors.toList());
        Map<Long, Map<Long, String>> reportAnswerMap = getReportAnswer(stationIdList,
            Arrays.asList(184L, 185L, 13L, 182L, 14L));
        Map<Long, Map<Long, String>> serviceReportAnswerMap = getServiceReportAnswer(reportIdList,
            Arrays.asList(155L, 157L));
        for (StationReport report : reports) {
            UserProfile userProfile = idToUserProfileMap.get(report.getUserId());

            StationRegularServiceReport serviceReport =
                idToRegularServiceReportMap.containsKey(report.getId().toString()) ?
                    idToRegularServiceReportMap.get(report.getId().toString()).get(0) : null;
            if (serviceReport != null) {
                //没有完成，无法后续跟进
                if (Long.valueOf(serviceReport.getAnswerId()).equals(179L)) {
                    String[] str = getStrArray(report, userProfile, reportAnswerMap, serviceReportAnswerMap);
                    noComplete_UnFollowUp.add(str);

                    VstarServiceReport_CompleteFollowUp completeFollowUp = new VstarServiceReport_CompleteFollowUp();
                    completeFollowUp.setStationName(report.getStation().getName());
                    completeFollowUp.setStationNum(report.getStation().getStationNum());
                    completeFollowUp.setCreateTime(report.getCreationTimeStr());
                    completeFollowUp.setId(report.getId());
                    completeFollowUp.setUserName(userProfile.getRealName());
                    completeFollowUp.setAnswer(serviceReport.getAnswerOther());
                    completeFollowUps.add(completeFollowUp);
                }
                //没有完成，需后续跟进
                if (Long.valueOf(serviceReport.getAnswerId()).equals(180L)) {
                    String[] str = getStrArray(report, userProfile, reportAnswerMap, serviceReportAnswerMap);
                    noComplete_needFollowUp.add(str);

                    VstarServiceReport_UnFinshedNoFollowUp unFinshedNoFollowUp = new VstarServiceReport_UnFinshedNoFollowUp();
                    unFinshedNoFollowUp.setStationName(report.getStation().getName());
                    unFinshedNoFollowUp.setStationNum(report.getStation().getStationNum());
                    unFinshedNoFollowUp.setCreateTime(report.getCreationTimeStr());
                    unFinshedNoFollowUp.setId(report.getId());
                    unFinshedNoFollowUp.setUserName(userProfile.getRealName());
                    unFinshedNoFollowUp.setAnswer(serviceReport.getAnswerOther());
                    unFinshedNoFollowUps.add(unFinshedNoFollowUp);
                }
                //已完成
                if (Long.valueOf(serviceReport.getAnswerId()).equals(181L)) {
                    String[] str = getStrArray(report, userProfile, reportAnswerMap, serviceReportAnswerMap);
                    completeReport.add(str);
                }
                //已完成，但需其他部门跟进
                if (Long.valueOf(serviceReport.getAnswerId()).equals(182L)) {
                    String[] str = getStrArray(report, userProfile, reportAnswerMap, serviceReportAnswerMap);
                    if (StringUtils.isNotEmpty(serviceReport.getAnswerOther())) {
                        String departmentId = serviceReport.getAnswerOther().split(",")[0];
                        Department department = idToDepartmentMap.get(Long.valueOf(departmentId));
                        if (null != department && StringUtils.isNotEmpty(department.getName())) {
                            str[14] = department.getName();
                        }
                    }

                    complete_needFollowUp.add(str);

                    VstarServiceReport_UnFinshedFollowUp unFinshedFollowUp = new VstarServiceReport_UnFinshedFollowUp();
                    unFinshedFollowUp.setStationName(report.getStation().getName());
                    unFinshedFollowUp.setStationNum(report.getStation().getStationNum());
                    unFinshedFollowUp.setCreateTime(report.getCreationTimeStr());
                    unFinshedFollowUp.setId(report.getId());
                    if (userProfile != null) {
                        unFinshedFollowUp.setUserName(userProfile.getRealName());
                    } else {
                        unFinshedFollowUp.setUserName("");
                    }
                    if (StringUtils.isNotEmpty(report.getDeparmentIds())) {
                        String[] departmentIds = report.getDeparmentIds().split(",");
                        StringBuffer stringBuffer = new StringBuffer("");
                        for (String id : departmentIds) {
                            if (StringUtils.isNotEmpty(id)) {
                                Department department = idToDepartmentMap.get(Long.valueOf(id));
                                if (department != null && StringUtils.isNotEmpty(department.getName())) {
                                    stringBuffer.append(department.getName() + ",");
                                }
                            }
                        }
                        unFinshedFollowUp.setAnswer(stringBuffer.toString());
                    }
                    unFinshedFollowUps.add(unFinshedFollowUp);
                }
                //处理产品问题（投诉）
                if (Long.valueOf(serviceReport.getAnswerId()).equals(246L)) {
                    String[] str = getStrArray(report, userProfile, reportAnswerMap, serviceReportAnswerMap);
                    manageProduct.add(str);
                }
                //计划进行该项工作，尚未开展
                if (Long.valueOf(serviceReport.getAnswerId()).equals(354L)) {
                    String[] str = getStrArray(report, userProfile, reportAnswerMap, serviceReportAnswerMap);
                    planReport.add(str);
                }

            }
        }

        results.setManageProduct(manageProduct);
        results.setNoComplete_UnFollowUp(noComplete_UnFollowUp);
        results.setNoComplete_needFollowUp(noComplete_needFollowUp);
        results.setCompleteReport(completeReport);
        results.setComplete_needFollowUp(complete_needFollowUp);
        results.setPlanReport(planReport);

        results.setNoComplete_UnableToFollowUp(noComplete_UnableToFollowUp.intValue());
        results.setNoComplete_NeedToFollowUp(noComplete_NeedToFollowUp.intValue());
        results.setComplete_NeedOthersToFollowUp(complete_NeedOthersToFollowUp.intValue());
        results.setComplete(complete.intValue());
        results.setManageProductNum(manageProductNum.intValue());
        results.setPlan(plan.intValue());

        results.setCompleteFollowUps(completeFollowUps);
        results.setUnFinshedNoFollowUps(unFinshedNoFollowUps);
        results.setUnFinshedFollowUps(unFinshedFollowUps);
        return results;
    }


    /**
     * 维修站id到问题到答案的映射
     * @param station
     * @param questionIdList
     * @return
     */
    private Map<Long, Map<Long, String>> getReportAnswer(List<Long> stationIdList,
        List<Long> questionIdList) {
        List<StationReport> stationReport = stationReportRepository.selectReport(stationIdList);
        if (CollectionUtils.isEmpty(stationReport)) {
            return new HashMap<>(0);
        }

        List<Long> reportIdList = stationReport.stream().map(StationReport::getId)
            .collect(Collectors.toList());

        Map<Long, StationReport> idToReportMap = stationReport.stream()
            .collect(Collectors.toMap(StationReport::getStationId,
                Function.identity()));

        List<StationColorReport> colorReport =
            stationColorReportRepository.selectReportQuestion(reportIdList, questionIdList);

        if (CollectionUtils.isEmpty(colorReport)) {
            return new HashMap<>(0);
        }

        List<String> answerIdList = new ArrayList<>(0);
        for (StationColorReport stationColorReport : colorReport) {

            if (StringUtils.isEmpty(stationColorReport.getAnswerOther())) {
                String answerId = stationColorReport.getAnswerId();
                String[] ids = answerId.split(",");
                for (String id : ids) {
                    if (StringUtils.isNotEmpty(id)) {
                        answerIdList.add(id);
                    }
                }
            }
        }
        List<Long> answerIdLongList = answerIdList.stream().map(Long::valueOf).collect(Collectors.toList());
        List<QuestionAnswerOption> byAnswerIdList = questionAnswerOptionRepository.findByAnswerIdList(
            answerIdLongList);
        Map<Long, QuestionAnswerOption> idToAnswerMap = byAnswerIdList.stream()
            .collect(Collectors.toMap(QuestionAnswerOption::getId, Function.identity()));

        Map<Long, List<StationColorReport>> reportIdToAnswerMap = colorReport.stream()
            .collect(Collectors.groupingBy(StationColorReport::getReportIdNotReport));

        Map<Long, Map<Long, String>> stationIdToQuestionIdToAnswerMap = new HashMap<>();
        for (StationReport report : stationReport) {
            // 遍历维修站报告
            Map<Long, String> questionToAnswerMap = new HashMap<>(0);
            List<StationColorReport> stationColorReportList =
                reportIdToAnswerMap.get(report.getId());

            // 遍历答案
            for (StationColorReport stationColorReport : stationColorReportList) {
                if (StringUtils.isNotEmpty(stationColorReport.getAnswerOther())) {
                    questionToAnswerMap.put(stationColorReport.getQuestionId(),
                        stationColorReport.getAnswerOther());
                } else {
                    String answerId = stationColorReport.getAnswerId();
                    String[] ids = answerId.split(",");
                    StringBuilder stringBuffer = new StringBuilder("");
                    for (String id : ids) {
                        QuestionAnswerOption answerOption = null;
                        if (StringUtils.isNotEmpty(id)) {
                            answerOption = idToAnswerMap.get(Long.valueOf(id));
                        }
                        if (answerOption != null&&answerOption.getName() != null) {
                            if (StringUtils.isNotEmpty(answerOption.getName())) {
                                stringBuffer.append(answerOption.getName()).append(",");
                            }
                        }
                    }
                    questionToAnswerMap.put(stationColorReport.getQuestionId(),
                        stringBuffer.toString());
                }
            }
            stationIdToQuestionIdToAnswerMap.put(report.getStationId(), questionToAnswerMap);
        }

        return stationIdToQuestionIdToAnswerMap;
    }

    //
    // private String getServiceReportAnswer(StationReport stationReport, Long questionId) {
    //     StationRegularServiceReport serviceReport = stationRegularServiceReportRepository.selectQuestionOther(stationReport.getId(), questionId);
    //     if (serviceReport == null) {
    //         return "";
    //     }
    //     StringBuffer stringBuffer = new StringBuffer("");
    //     String answerId = serviceReport.getAnswerId();
    //     if (StringUtils.isNotEmpty(answerId)) {
    //         String[] ids = answerId.split(",");
    //         for (String id : ids) {
    //             QuestionAnswerOption answerOption = null;
    //             if (StringUtils.isNotEmpty(id)) {
    //                 answerOption = questionAnswerOptionRepository.findOne(Long.valueOf(id));
    //             }
    //             if (answerOption != null && StringUtils.isNotEmpty(answerOption.getName())) {
    //                 stringBuffer.append(answerOption.getName());
    //                 if (id.equals("191") || id.equals("189")) {
    //                     stringBuffer.append("：");
    //                 } else {
    //                     stringBuffer.append(",");
    //                 }
    //             }
    //             if (id.equals("191") && StringUtils.isNotEmpty(serviceReport.getAnswerOther())) {
    //                 stringBuffer.append(serviceReport.getAnswerOther()).append(",");
    //             }
    //             if (id.equals("189") && StringUtils.isNotEmpty(stationReport.getAnswerOther())) {
    //                 stringBuffer.append(stationReport.getAnswerOther()).append(",");
    //             }
    //         }
    //     } else {
    //         stringBuffer.append(serviceReport.getAnswerOther());
    //     }
    //     return stringBuffer.toString();
    // }

    private Map<Long, Map<Long, String>> getServiceReportAnswer(List<Long> reportIdList,
        List<Long> questionIdList) {
        List<StationRegularServiceReport> serviceReportList =
            stationRegularServiceReportRepository.selectQuestionOther(reportIdList, questionIdList);
        if (CollectionUtils.isEmpty(serviceReportList)) {
            return new HashMap<>(0);
        }

        List<String> answerIdList = new ArrayList<>(16);
        for (StationRegularServiceReport stationRegularServiceReport : serviceReportList) {

            String answerId = stationRegularServiceReport.getAnswerId();
            if (StringUtils.isNotEmpty(answerId)) {
                String[] ids = answerId.split(",");
                for (String id : ids) {
                    if (StringUtils.isNotEmpty(id)) {
                        answerIdList.add(id);
                    }
                }
            }
        }
        List<Long> answerIdLongList = answerIdList.stream().map(Long::valueOf).collect(Collectors.toList());
        List<QuestionAnswerOption> byAnswerIdList = questionAnswerOptionRepository.findByAnswerIdList(
            answerIdLongList);
        Map<Long, QuestionAnswerOption> idToAnswerMap = byAnswerIdList.stream()
            .collect(Collectors.toMap(QuestionAnswerOption::getId, Function.identity()));

        Map<Long, Map<Long, String>> reportIdToQuestionToAnswer = new HashMap<>();
        for (StationRegularServiceReport stationRegularServiceReport : serviceReportList) {

            Map<Long, String> questionToAnswerMap = reportIdToQuestionToAnswer.computeIfAbsent(
                stationRegularServiceReport.getStationReport().getStationId(),
                item -> new HashMap<>(4));

            String answerId = stationRegularServiceReport.getAnswerId();
            StringBuffer stringBuffer = new StringBuffer("");
            if (StringUtils.isNotEmpty(answerId)) {
                String[] ids = answerId.split(",");
                for (String id : ids) {
                    QuestionAnswerOption answerOption = null;
                    if (StringUtils.isNotEmpty(id)) {
                        answerOption = idToAnswerMap.get(Long.valueOf(id));
                    }
                    if (answerOption != null && StringUtils.isNotEmpty(answerOption.getName())) {
                        stringBuffer.append(answerOption.getName());
                        if (id.equals("191") || id.equals("189")) {
                            stringBuffer.append("：");
                        } else {
                            stringBuffer.append(",");
                        }
                    }
                    if (id.equals("191") && StringUtils.isNotEmpty(stationRegularServiceReport.getAnswerOther())) {
                        stringBuffer.append(stationRegularServiceReport.getAnswerOther()).append(",");
                    }
                    if (id.equals("189") && StringUtils.isNotEmpty(stationRegularServiceReport.getAnswerOther())) {
                        stringBuffer.append(stationRegularServiceReport.getAnswerOther()).append(",");
                    }
                }
            } else {
                stringBuffer.append(stationRegularServiceReport.getAnswerOther());
            }
            questionToAnswerMap.put(stationRegularServiceReport.getQuestionId(),
                stringBuffer.toString());
        }
        return reportIdToQuestionToAnswer;
    }


    private String[] getStrArray(StationReport report, UserProfile userProfile, Map<Long,
        Map<Long, String>> sttaionIdToQuestionIdToAnswerMap, Map<Long,
        Map<Long, String>> serviceReportIdToQuestionIdToAnswerMap) {
        String[] str = new String[15];
        str[0] = report.getCreationTimeStr();
        str[1] = userProfile.getRealName();
        str[2] = report.getStation().getProvince();
        str[3] = report.getStation().getName();
        str[4] = report.getStation().getCategory();
        str[5] = report.getStation().getBrand();
        str[6] = report.getStation().getStationNum();
        Map<Long, String> longStringMap = sttaionIdToQuestionIdToAnswerMap.getOrDefault(
            report.getStationId(), new HashMap<>(0));
        Map<Long, String> serviceAnswerMap = serviceReportIdToQuestionIdToAnswerMap.getOrDefault(
            report.getId(), new HashMap<>(0));
        str[7] = longStringMap.getOrDefault(184L, "");
        str[8] = longStringMap.getOrDefault(185L, "");
        str[9] = longStringMap.getOrDefault(13L, "");
        str[10] = longStringMap.getOrDefault(182L, "");
        str[11] = longStringMap.getOrDefault(14L, "");
        str[12] = serviceAnswerMap.getOrDefault(155L, "");
        str[13] = serviceAnswerMap.getOrDefault(157L, "");
        str[14] = "";
        return str;
    }

    @Autowired
    private DepartmentRepository departmentRepository;
    //筛选条件
    public List<StationReport> getStationReports(String province, String city, String brand, String category, String stationName,
                                                Long serviceStartTime, Long serviceEndTime, Long startTime, Long endTime, String serviceType,
                                                Integer serviceNum, String reportStatus,String userName, Boolean type, String serviceReportStatus, Long userId, Boolean paintType,
                                                String customerStat, String customerType, String paintBrand, String productPosition, String stationNum, String department,String payService,String sentTime,String serviceTime) {
        String[] categories = null;
        String[] serviceTypes = null;
        String[] serviceReportStatuses = null;
        Long[] customerStats = null;
        Long[] customerTypes = null;
        String[] cities = null;
        String[] reportStatuses = null;
        String[] productPositions = null;
        String[] departments = null;


        if (StringUtils.isNotEmpty(category)){
            categories = category.split(",");
        }
        if (StringUtils.isNotEmpty(serviceType)){
            serviceTypes = serviceType.split(",");
        }
        if (StringUtils.isNotEmpty(serviceReportStatus)){
            serviceReportStatuses = serviceReportStatus.split(",");
        }
        if (StringUtils.isNotEmpty(city)){
            cities = city.split(",");
        }
        if (StringUtils.isNotEmpty(reportStatus)){
            reportStatuses = reportStatus.split(",");
        }
        if (!StringUtils.isEmpty(productPosition)){
            productPositions = productPosition.split(",");
        }
        if (!StringUtils.isEmpty(department)){
            departments = department.split(",");
        }
        if (!StringUtils.isEmpty(customerStat)){
            customerStats = (Long[]) ConvertUtils.convert(customerStat.split(","),Long.class);
        }
        if (!StringUtils.isEmpty(customerType)){
            customerTypes = (Long[]) ConvertUtils.convert(customerType.split(","),Long.class);
        }

        StationReportExample stationReportExample = new StationReportExample();
        StationReportExample.Criteria stationReportExampleCriteria = stationReportExample.createCriteria();
        stationReportExampleCriteria.andDeletedEqualTo(false);
        stationReportExampleCriteria.andEnabledEqualTo(true);
        stationReportExampleCriteria.andReportTypeEqualTo(Constants.REPORT_TYPE_COMPLETE);
        stationReportExampleCriteria.andCategoryEqualTo(Constants.SERVICE_REPORT);

        StationsExample stationsExample = new StationsExample();
        Criteria stationsExampleCriteria = stationsExample.createCriteria();
        stationsExampleCriteria.andCheckStationEqualTo(true);

        UsersExample usersExample = new UsersExample();
        UsersExample.Criteria usersExampleCriteria = usersExample.createCriteria();

        // builder.and(qStationReport.station.checkStation.eq(true));
        User user = userRepository.findOne(userId);
        UserProfile userProfile = userProfileRepository.findByUserId(userId);
        SearchVoId searchVoId;
        if (user.getUserType().equals(UserType.dealers)) {
            searchVoId = searchPermissionsService.getDealersStationId(userProfile, province, brand);
        } else {
            searchVoId = searchPermissionsService.searchColorReportByProvinceBrand(userProfile, province, brand);
        }
        if (searchVoId.getResultStationId().isEmpty()) {
            return new ArrayList<>();
        }
        stationsExampleCriteria.andIdIn(searchVoId.getResultStationId());
        // builder.and(qStationReport.station.id.in(searchVoId.getResultStationId()));
        if (StringUtils.isNotEmpty(stationNum)) {
            stationsExampleCriteria.andStationNumLike(stationNum);
            // builder.and(qStationReport.station.stationNum.like("%" + stationNum + "%"));
        }
        if (paintType != null){
            stationsExampleCriteria.andIsPaintEqualTo(paintType?1:0);
            // builder.and(qStationReport.station.isPaint.eq(paintType?1:0));
        }
        if (cities!=null) {
            stationsExampleCriteria.andCityIn(Arrays.asList(cities));
            // builder.and(qStationReport.station.city.in(cities));
        }
        if (null != categories) {
            stationsExampleCriteria.andCategoryIn(Arrays.asList(categories));
            // builder.and(qStationReport.station.category.in(categories));
        }
        if (StringUtils.isNotEmpty(stationName)) {
            stationsExampleCriteria.andNameLike(stationName);
            // builder.and(qStationReport.station.name.like("%" + stationName + "%"));
        }
        if (customerStats != null) {
            stationsExampleCriteria.andCustomerStatIn(Arrays.asList(customerStats));
            // builder.and(qStationReport.station.customerStat.in(customerStats));
        }

        if (customerTypes != null) {
            stationsExampleCriteria.andCustomerTypeIn(Arrays.asList(customerTypes));
            // builder.and(qStationReport.station.customerType.in(customerTypes));
        }
        if (StringUtils.isNotEmpty(paintBrand)) {
            stationsExampleCriteria.andPaintBrandLike(paintBrand);
            // builder.and(qStationReport.station.paintBrand.like("%" + paintBrand + "%"));
        }
        if (productPositions!=null) {
            // BooleanBuilder ppBuilder = new BooleanBuilder();
            // for (String pp:productPositions){
            //     ppBuilder.or(qStationReport.station.productPosition.like("%" + pp + "%"));
            // }
            // builder.and(ppBuilder);
            stationsExampleCriteria.andProductPositionListLike(Arrays.asList(productPositions));
        }
        if (!CollectionUtils.isEmpty(stationsExampleCriteria.getAllCriteria())) {
            List<Stations> stationsList = stationsMapper.selectByExample(stationsExample);
            if (CollectionUtils.isEmpty(stationsList)) {
                new ArrayList<>(0);
            }
            List<Long> stationIdList = stationsList.stream().map(Stations::getId)
                .collect(Collectors.toList());
            stationReportExampleCriteria.andStationIdIn(stationIdList.stream().distinct().collect(
                Collectors.toList()));
        }

        if (serviceNum != null && serviceNum != 0) {
            // builder.and(qStationReport.user.serviceNum.eq(serviceNum));
            usersExampleCriteria.andServicenumEqualTo(serviceNum);
        }

        if (reportStatuses!=null){
            // builder.and(qStationReport.exceptionType.in(reportStatuses));
            stationReportExampleCriteria.andExceptionTypeIn(Arrays.asList(reportStatuses));
        }
        if (serviceTypes != null || serviceReportStatuses != null ) {
            //先从报告答案中找出所有填写过ids中任何一个id的答案
            StationRegularServiceReportExample stationRegularServiceReportExample = new StationRegularServiceReportExample();
            StationRegularServiceReportExample.Criteria criteria = stationRegularServiceReportExample.createCriteria();
            criteria.andDeletedEqualTo(false);
            criteria.andEnabledEqualTo(true);
            if (serviceTypes != null) {
                criteria.andAnswerIdLike(Arrays.asList(serviceTypes));
            }
            if (serviceReportStatuses != null) {
                criteria.andAnswerIdLike(Arrays.asList(serviceReportStatuses));
            }
            List<com.ugoodtech.mdcc.core.model.entity.StationRegularServiceReport> stationRegularServiceReportList = stationRegularServiceReportMapper.selectByExample(
                stationRegularServiceReportExample);
            if (stationRegularServiceReportList == null || stationRegularServiceReportList.size() == 0){
                return new ArrayList<>();
            }
            List<Long> ids = stationRegularServiceReportList.stream().map(
                    com.ugoodtech.mdcc.core.model.entity.StationRegularServiceReport::getReportId)
                .filter(Objects::nonNull).collect(
                    Collectors.toList());
            stationReportExampleCriteria.andIdIn(ids.stream().distinct().collect(Collectors.toList()));
        }
        if (StringUtils.isNotEmpty(userName) || departments != null) {
            boolean flag = false;
            if (StringUtils.isNotEmpty(userName)) {
                flag = true;
            }
            if (departments != null){
                flag = true;
            }
            if (flag) {
                List<UserProfiles> userProfileList = mybatisUserService.listUser(userName, departments);
                    if (userProfileList == null || userProfileList.size() == 0) {
                    return new ArrayList<>();
                }
                List<Long> userIds = new ArrayList<>();
                for (UserProfiles userp : userProfileList) {
                    userIds.add(userp.getUserId());
                }

                List<com.ugoodtech.mdcc.core.model.entity.StationColorReport> stationColorReportList = mybatisStationReportService.listColorReport(
                    userIds);
                if (stationColorReportList == null || stationColorReportList.size() == 0) {
                    return new ArrayList<>();
                }
                List<Long> ids = new ArrayList<>();
                for (com.ugoodtech.mdcc.core.model.entity.StationColorReport report : stationColorReportList) {
                    if (report.getReportId() != null) {
                        ids.add(report.getReportId());
                    }
                }
                stationReportExampleCriteria.andIdIn(ids.stream().distinct().collect(Collectors.toList()));
            }

        }

        if (serviceStartTime != null && serviceStartTime != 0){
            stationReportExampleCriteria.andStartServiceTimeGreaterThanOrEqualTo(
                new Date(serviceStartTime));
            // builder.and(qStationReport.startServiceTime.goe(new Date(serviceStartTime)));
        }
        if(serviceEndTime != null && serviceEndTime != 0) {
            // builder.and(qStationReport.endServiceTime.lt(new Date(serviceEndTime)));
            stationReportExampleCriteria.andEndServiceTimeLessThan(
                new Date(serviceEndTime));
        }

        if (startTime != null && startTime != 0){
            // builder.and(qStationReport.updateTime.goe(new Date(startTime)));
            stationReportExampleCriteria.andUpdateTimeGreaterThanOrEqualTo(
                new Date(startTime));
        }if(endTime != null && endTime != 0) {
            // builder.and(qStationReport.updateTime.lt(new Date(endTime)));
            stationReportExampleCriteria.andUpdateTimeLessThan(
                new Date(endTime));
        }
        if (StringUtils.isNotEmpty(payService) || StringUtils.isNotEmpty(sentTime) || StringUtils.isNotEmpty(serviceTime)){
            QStationColorReport qStationColorReport = QStationColorReport.stationColorReport;
            BooleanBuilder builderStationColor = new BooleanBuilder();
            builderStationColor.and(qStationColorReport.deleted.eq(false));
            builderStationColor.and(qStationColorReport.enabled.eq(true));
            boolean flag = false;
            if (StringUtils.isNotEmpty(payService)){
                flag = true;
            }
            if (StringUtils.isNotEmpty(sentTime)){
                flag = true;
            }
            if (StringUtils.isNotEmpty(serviceTime)){
                flag = true;
            }
            if (flag) {
                List<com.ugoodtech.mdcc.core.model.entity.StationColorReport> stationColorReportList = mybatisStationReportService.listColorReport(
                    payService, sentTime, serviceTime);
                if (stationColorReportList == null || stationColorReportList.size() == 0) {
                    return new ArrayList<>();
                }
                List<Long> stationReportIdList = stationColorReportList.stream().map(
                    com.ugoodtech.mdcc.core.model.entity.StationColorReport::getReportId).collect(
                    Collectors.toList());
                List<com.ugoodtech.mdcc.core.model.entity.StationReport> stationReportList = mybatisStationReportService.listReport(
                    stationReportIdList);
                if (CollectionUtils.isEmpty(stationReportList)) {
                    return new ArrayList<>(0);
                }
                List<Long> Ids = new ArrayList<>();
                for (com.ugoodtech.mdcc.core.model.entity.StationReport report : stationReportList) {
                    Ids.add(report.getStationId());
                }
                stationReportExampleCriteria.andStationIdIn(Ids.stream().distinct().collect(
                    Collectors.toList()));
            }
        }
        if (type != null) {
            List<com.ugoodtech.mdcc.core.model.entity.CustomerRegularReport> customerRegularReportList = mybatisStationReportService.listCustomerRegularReport(
                type ? 1 : 0);
            if (customerRegularReportList == null || customerRegularReportList.size()==0){
                return new ArrayList<>();
            }
            List<Long> regularReportIdList = customerRegularReportList.stream().map(
                    com.ugoodtech.mdcc.core.model.entity.CustomerRegularReport::getRegularReportId)
                .collect(Collectors.toList());

            stationReportExampleCriteria.andIdIn(regularReportIdList.stream().distinct().collect(
                Collectors.toList()));
        }
        List<com.ugoodtech.mdcc.core.model.entity.StationReport> stationReportList = stationReportMapper.selectByExample(
            stationReportExample);
        if (CollectionUtils.isEmpty(stationReportList)) {
            return new ArrayList<>(0);
        }
        List<Long> stationReportIdList = stationReportList.stream()
            .map(com.ugoodtech.mdcc.core.model.entity.StationReport::getId).distinct()
            .collect(
                Collectors.toList());
        QStationReport qStationReport = QStationReport.stationReport;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qStationReport.id.in(stationReportIdList));

        return (List<StationReport>) stationReportRepository.findAll(builder);
    }


    //筛选条件
    @Override
    public List<StationReport> getStationReport(String provinces, String brand, String category, String name,
                                                Long serviceStartTime, Long serviceEndTime, Long startTime, Long endTime, Long serviceType,
                                                Integer serviceNum, String userName, String isFollowUp, String followType, Long userId, String userType, Boolean isCustomer,
                                                Long customerStat, Long customerType, String parintBrand, String productPosition, String stationNum, Long departmentId) {
        QStationReport qStationReport = QStationReport.stationReport;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qStationReport.deleted.eq(false));
        builder.and(qStationReport.enabled.eq(true));
        builder.and(qStationReport.reportType.eq(Constants.REPORT_TYPE_COMPLETE));
        builder.and(qStationReport.category.eq(Constants.SERVICE_REPORT));
//        builder.and(qStationReport.station.checkStation.eq(true));
        User user = userRepository.findOne(userId);
        UserProfile userProfile = userProfileRepository.findByUserId(userId);
        SearchVoId searchVoId;
        if (user.getUserType().equals(UserType.dealers)) {
            searchVoId = searchPermissionsService.getDealersStationId(userProfile, provinces, brand);
        } else {
            searchVoId = searchPermissionsService.searchColorReportByProvinceBrand(userProfile, provinces, brand);
        }
        if (searchVoId.getResultStationId().isEmpty()) {
            return new ArrayList<>();
        }
        builder.and(qStationReport.station.id.in(searchVoId.getResultStationId()));
        if (StringUtils.isNotEmpty(stationNum)) {
            builder.and(qStationReport.station.stationNum.like("%" + stationNum + "%"));
        }
        if (StringUtils.isNotEmpty(category)) {
            builder.and(qStationReport.station.category.eq(category));
        }
        if (StringUtils.isNotEmpty(name)) {
            builder.and(qStationReport.station.name.like("%" + name + "%"));
        }
        if (serviceType != null && serviceType != 0) {
            builder.and(qStationReport.serviceTypAnswerIds.like("%" + serviceType + "%"));
        }
        if (serviceNum != null && serviceNum != 0) {
            builder.and(qStationReport.user.serviceNum.eq(serviceNum));
        }
        if (customerStat != null && customerStat != 0) {
            builder.and(qStationReport.station.customerStat.eq(customerStat));
        }
        if (customerType != null && customerType != 0) {
            builder.and(qStationReport.station.customerType.eq(customerType));
        }
        if (StringUtils.isNotEmpty(parintBrand)) {
            builder.and(qStationReport.station.paintBrand.like("%" + parintBrand + "%"));
        }
        if (StringUtils.isNotEmpty(productPosition)) {
            builder.and(qStationReport.station.productPosition.like("%" + productPosition + "%"));
        }

        if (StringUtils.isNotEmpty(userName) || departmentId != null && departmentId != 0) {
            List<Long> departmentIds = new ArrayList<>();
            QStationColorReport qStationColorReport = QStationColorReport.stationColorReport;
            QUserProfile qUserProfile = QUserProfile.userProfile;
            BooleanBuilder booleanBuilder = new BooleanBuilder();
            booleanBuilder.and(qUserProfile.deleted.eq(false));
            booleanBuilder.and(qUserProfile.enabled.eq(true));
            booleanBuilder.and(qUserProfile.user.userFlag.in(0,2));
            boolean flag = false;
            if (departmentId != null && departmentId != 0) {
                flag = true;
                List<Long> parentDeparement = departmentCoreService.parentDepartment();
                if (parentDeparement.contains(departmentId)) {
                    departmentIds = departmentCoreService.getLowerDepartment(departmentId);
                } else {
                    departmentIds.add(departmentId);
                }
                booleanBuilder.and(qUserProfile.departmentList.any().id.in(departmentIds));
            }
            if (StringUtils.isNotEmpty(userName)) {
                flag = true;
                booleanBuilder.and(qUserProfile.realName.like("%" + userName + "%"));
            }
            if (flag) {
                List<UserProfile> userProfileList = (List<UserProfile>) userProfileRepository.findAll(booleanBuilder);
                if (userProfileList == null || userProfileList.size() == 0) {
                    return new ArrayList<>();
                }
                List<Long> userIds = new ArrayList<>();
                for (UserProfile userp : userProfileList) {
                    userIds.add(userp.getUser().getId());
                }
                BooleanBuilder colorBuilder = new BooleanBuilder();
                colorBuilder.and(qStationColorReport.deleted.eq(false));
                colorBuilder.and(qStationColorReport.enabled.eq(true));
                colorBuilder.and(qStationColorReport.stationReport.station.report_finished.in(1, 2, 3));
                colorBuilder.and(qStationColorReport.stationReport.user.id.in(userIds));
                List<StationColorReport> list = (List<StationColorReport>) stationColorReportRepository.findAll(colorBuilder);
                if (list == null || list.size() == 0) {
                    return new ArrayList<>();
                }
                List<Long> ids = new ArrayList<>();
                for (StationColorReport report : list) {
                    if (report.getReportId() != null) {
                        ids.add(Long.parseLong(report.getReportId()));
                    }
                }
                builder.and(qStationReport.id.in(ids));
            }
        }

        if (serviceStartTime != null && serviceStartTime != 0 && serviceEndTime != null && serviceEndTime != 0) {
            builder.and(qStationReport.startServiceTime.after(new Date(serviceStartTime)));
            builder.and(qStationReport.endServiceTime.before(new Date(serviceEndTime)));
        }

        if (startTime != null && startTime != 0 && endTime != null && endTime != 0) {
            builder.and(qStationReport.updateTime.after(new Date(startTime)));
            builder.and(qStationReport.updateTime.before(new Date(endTime)));
        }

        List<StationReport> reports = (List<StationReport>) stationReportRepository.findAll(builder);
        return reports;
    }

}
